﻿using System;
using Infragistics.Win.UltraWinGrid;
using POS.BusinessComponents;
using POS.BusinessEntities;
using System.Windows.Forms;

namespace POS.UI.Mantenimientos
{
    public partial class MantMarcasForm : MantForm
    {
        #region Declarations

        private int selectedId;
        private const int idColumn = 0;
        private const int nameColumn = 1;
        private readonly BCEntidad bcEntidad = new BCEntidad(TipoEntidad.Marca);

        #endregion

        #region Constructors

        public MantMarcasForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        private void MantMarcasForm_Load(object sender, EventArgs e)
        {
            LoadList();
            toolStrip.ItemClicked += toolStrip_ItemClicked;
        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case NewText:
                    New();
                    break;
            }
        }

        private void grid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ConfigureGrid((UltraGrid)sender);
        }

        private void grid_AfterRowActivate(object sender, EventArgs e)
        {
            selectedId = (int)grid.ActiveRow.Cells[idColumn].Value;
            nombreTextBox.Text = grid.ActiveRow.Cells[nameColumn].Value.ToString();
        }

        private void grid_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
        {
            EnableDisable();
        }
        
        private void grid_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete && grid.Selected.Rows.Count > 0)
                Delete();
        }

        #endregion

        #region Methods: Non-Public

        private void LoadList()
        {
            grid.DataSource = bcEntidad.GetList("");
            ClearGridSelection();
        }

        private void ClearGridSelection()
        {
            grid.Selected.Rows.Clear();
            grid.ActiveRow = null;
            EnableDisable();
        }

        protected override bool ValidateInput()
        {
            var name = nombreTextBox.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                ShowError("Debe ingresar el nombre");
                return false;
            }
            if (!IsUniqueName(TipoEntidad.Marca, name) && selectedId == 0)
            {
                ShowError("El nombre seleccionado ya ha sido ingresado");
                return false;
            }
            return true;
        }
        
        private void EnableDisable()
        {
            toolStrip.Items[DeleteText].Enabled = grid.Selected.Rows.Count > 0;
        }
        
        protected override void Save()
        {
            if (ValidateInput())
            {
                bcEntidad.Save(Pack());
                New();
                LoadList();
            }
        }

        protected override void Delete()
        {
            if (ShowQuestion("¿Está seguro de querer eliminar la marca seleccionada?"))
            {   
                bcEntidad.Delete(selectedId);
                LoadList();
                ClearControls(Controls);
                ClearGridSelection();
                grid.Select();
            }
        }

        private void New()
        {
            ClearControls(Controls);
            nombreTextBox.Focus();
            selectedId = 0;
            ClearGridSelection();
        }
        
        private BEEntidad Pack()
        {
            var marca = new BEEntidad
                            {
                                Id = selectedId,
                                Nombre = nombreTextBox.Text.Trim()
                            };
            return marca;
        }

        #endregion
    }
}
